﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;

namespace QuanLyHocSinh.GUI
{
    public partial class F_QuanLyTaiKhoan : DevExpress.XtraEditors.XtraUserControl
    {
        int trangthaiform = 1;
        List<TextEdit> TbParams = new List<TextEdit>();
        public F_QuanLyTaiKhoan()
        {
            InitializeComponent();
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        }

        private void DuaTextBoxVaoList()
        {
            TbParams.Add(txtUserName);
            TbParams.Add(txtUserNameS);
        }

        private void F_QuanLyTaiKhoan_Load(object sender, EventArgs e)
        {
            LietKeDanhSachNhanVien();
            cbVaiTroS.Properties.TextEditStyle = cbVaiTro.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor; 
        }

        private void LietKeDanhSachNhanVien()
        {
            this.dgvNguoiDung.DataSource = BUSNguoiDung.DanhSachTaiKhoan();
            this.cbVaiTro.SelectedText = this.cbVaiTroS.SelectedText = "";

        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                if (trangthaiform == 1)
                {
                    DTONguoiDung nguoidung = new DTONguoiDung();
                    nguoidung.UserName = txtUserNameS.Text;

                    if (cbVaiTroS.SelectedItem != null)
                        nguoidung.VaiTro = cbVaiTroS.SelectedItem.ToString();
                    else
                        nguoidung.VaiTro = "";

                    if (ceTrangThaiS.Checked)
                        nguoidung.TrangThai = true;
                    else
                        nguoidung.TrangThai = false;
                    dgvNguoiDung.DataSource = BUSNguoiDung.TimKiemTaiKhoan(nguoidung);
                }
                else
                {
                    XtraMessageBox.Show("Vui Lòng Hoàn Tất Chức Năng Đang Thực Hiện", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);    
            }
        }

        private void btnXoaThongTin_Click(object sender, EventArgs e)
        {
            if (trangthaiform == 1)
            {
                this.txtUserNameS.Text = null;
                this.cbVaiTroS.SelectedItem = null;
                this.dgvNguoiDung.DataSource = BUSNguoiDung.DanhSachTaiKhoan();
            }
            else
            {
                XtraMessageBox.Show("Vui Lòng Hoàn Tất Chức Năng Đang Thực Hiện", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtUserName.Text != "")
                {
                    if (cbVaiTro.SelectedItem != null)
                    {
                        DTONguoiDung nguoidung = new DTONguoiDung();
                        nguoidung.UserName = txtUserName.Text;
                        nguoidung.VaiTro = cbVaiTro.SelectedItem.ToString();
                        if (ceTrangThai.Checked)
                            nguoidung.TrangThai = true;
                        else
                            nguoidung.TrangThai = false;
                        int ketqua = BUSNguoiDung.ThemTaiKhoan(nguoidung);
                        if (ketqua == 1)
                        {
                            this.txtUserName.Text = "";
                            this.ceTrangThai.Checked = false;
                            this.cbVaiTro.SelectedItem = null;
                            XtraMessageBox.Show("Thêm Thành Công Tài Khoản Người Dùng!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.dgvNguoiDung.DataSource = BUSNguoiDung.DanhSachTaiKhoan();
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("Vui lòng chọn vai trò", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    XtraMessageBox.Show("Vui lòng điền Username", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvTaiKhoan_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try
            {
                this.trangthaiform = 0;
                this.lbUserID.Text = gvTaiKhoan.GetRowCellValue(e.RowHandle, gvTaiKhoan.Columns[0]).ToString().Trim();
                this.txtUserName.Text = gvTaiKhoan.GetRowCellValue(e.RowHandle, gvTaiKhoan.Columns[1]).ToString().Trim();
                this.lbUserName.Text = this.txtUserName.Text = gvTaiKhoan.GetRowCellValue(e.RowHandle, gvTaiKhoan.Columns[1]).ToString().Trim();
                this.cbVaiTro.SelectedItem = gvTaiKhoan.GetRowCellValue(e.RowHandle, gvTaiKhoan.Columns[2]).ToString().Trim();
                this.ceTrangThai.Checked = bool.Parse(gvTaiKhoan.GetRowCellValue(e.RowHandle, gvTaiKhoan.Columns[3]).ToString().Trim());
                this.cmdThem.Enabled = false;
                this.cmdSua.Enabled = this.cmdXoa.Enabled = this.cmdBoQua.Enabled = this.btnResetPassword.Visible = true;
            }
            catch (Exception ex)
            {
 
            }
        }

        private void cmdBoQua_Click(object sender, EventArgs e)
        {
            this.trangthaiform = 1;
            this.txtUserName.Text = "";
            this.ceTrangThai.Checked = false;
            this.cbVaiTro.SelectedItem = null;
            this.cmdThem.Enabled = true;
            this.cmdSua.Enabled = this.cmdXoa.Enabled = this.cmdBoQua.Enabled = false;
            this.dgvNguoiDung.DataSource = BUSNguoiDung.DanhSachTaiKhoan();
        }

        private void cmdSua_Click(object sender, EventArgs e)
        {
            try
            {
                DTONguoiDung nguoidung = new DTONguoiDung();
                nguoidung.UserId = int.Parse(lbUserID.Text);

                if (txtUserName.Text != lbUserName.Text)
                    nguoidung.UserName = txtUserName.Text;
                else
                    nguoidung.UserName = "";

                if (cbVaiTro.SelectedItem != null)
                    nguoidung.VaiTro = cbVaiTro.SelectedItem.ToString();
                else
                    nguoidung.VaiTro = "";

                if (ceTrangThai.Checked)
                    nguoidung.TrangThai = true;
                else
                    nguoidung.TrangThai = false;
                int ketqua = BUSNguoiDung.CapNhatTaiKhoan(nguoidung);
                if (ketqua == 1)
                {
                    this.trangthaiform = 1;
                    this.txtUserName.Text = "";
                    this.ceTrangThai.Checked = false;
                    this.cbVaiTro.SelectedItem = null;
                    this.dgvNguoiDung.DataSource = BUSNguoiDung.DanhSachTaiKhoan();
                    this.cmdThem.Enabled = true;
                    this.cmdXoa.Enabled = this.cmdSua.Enabled = this.cmdBoQua.Enabled = this.btnResetPassword.Visible = false;
                    XtraMessageBox.Show(string.Format("Đã Cập Nhật Thành Công Thông Tin Cho Tài Khoản {0}",lbUserName.Text), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmdXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialogResult = XtraMessageBox.Show(string.Format("Bạn Có Chắc Muốn Xóa Tài Khoản: {0} ?",lbUserName.Text), "Thông Báo", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    DTONguoiDung nguoidung = new DTONguoiDung();
                    nguoidung.UserId = int.Parse(lbUserID.Text);
                    int ketqua = BUSNguoiDung.XoaTaiKhoan(nguoidung);
                    if (ketqua == 1)
                    {
                        this.trangthaiform = 1;
                        this.txtUserName.Text = "";
                        this.ceTrangThai.Checked = false;
                        this.cbVaiTro.SelectedItem = null;
                        this.dgvNguoiDung.DataSource = BUSNguoiDung.DanhSachTaiKhoan();
                        this.cmdThem.Enabled = true;
                        this.cmdXoa.Enabled = this.cmdSua.Enabled = this.cmdBoQua.Enabled = this.btnResetPassword.Visible = false;
                        XtraMessageBox.Show(string.Format("Đã Xóa Thành Công Tài Khoản {0}", lbUserName.Text), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else if (dialogResult == DialogResult.No)
                {
                    XtraMessageBox.Show("OK", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnResetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialogResult = XtraMessageBox.Show(string.Format("Bạn Có Chắc Muốn Reset Mật Khẩu Của Tài Khoản: {0} ?",lbUserName.Text), "Thông Báo", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    DTONguoiDung nguoidung = new DTONguoiDung();
                    nguoidung.UserId = int.Parse(lbUserID.Text);
                    nguoidung.Password = "e10adc3949ba59abbe56e057f20f883e";
                    int ketqua = BUSNguoiDung.CapNhatMatKhau(nguoidung);
                    if (ketqua == 1)
                    {
                        this.trangthaiform = 1;
                        this.txtUserName.Text = "";
                        this.ceTrangThai.Checked = false;
                        this.cbVaiTro.SelectedItem = null;
                        this.dgvNguoiDung.DataSource = BUSNguoiDung.DanhSachTaiKhoan();
                        this.cmdThem.Enabled = true;
                        this.cmdXoa.Enabled = this.cmdSua.Enabled = this.cmdBoQua.Enabled = this.btnResetPassword.Visible = false;
                        XtraMessageBox.Show(string.Format("Đã Rest Mật Khẩu Cho Tài Khoản : {0}!", lbUserName.Text), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

       
    }
}
